/*
 * SetStateCategoryForm.java
 *
 * Created on November 20, 2008, 11:25 AM
 */

package rrs;
import java.sql.*;
/**
 *
 * @author  Tucco78
 */
public class SetStateCategoryForm extends javax.swing.JFrame {

    /** Creates new form SetStateCategoryForm */
    public SetStateCategoryForm() {
        initComponents();
        conn = DBInterface.dbConnOpen();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtEnterCategoryID = new javax.swing.JTextField();
        btnActivate = new javax.swing.JButton();
        btnDeactivate = new javax.swing.JButton();
        lblCategory = new javax.swing.JLabel();
        lblmisc = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        txtEnterCategoryID.setName("txtEnterCategoryID"); // NOI18N

        btnActivate.setText("Activate");
        btnActivate.setName("btnActivate"); // NOI18N
        btnActivate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnActivateMouseClicked(evt);
            }
        });

        btnDeactivate.setText("Deactivate");
        btnDeactivate.setName("btnDeactivate"); // NOI18N
        btnDeactivate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnDeactivateMouseClicked(evt);
            }
        });

        lblCategory.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblCategory.setText("Enter Category Price Scheme ID");
        lblCategory.setName("lblCategory"); // NOI18N

        lblmisc.setName("lblmisc"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(99, 99, 99)
                        .addComponent(txtEnterCategoryID, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblmisc))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(57, 57, 57)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblCategory)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnActivate, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(44, 44, 44)
                                .addComponent(btnDeactivate)))))
                .addContainerGap(35, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addComponent(lblCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtEnterCategoryID, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnActivate, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDeactivate, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(lblmisc)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnActivateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnActivateMouseClicked
 if (btnActivate.isEnabled() == true)
  {     
    String rtn = "";
    String id = txtEnterCategoryID.getText();
    try
     {
        if (determineState(id))
        {
            lblmisc.setText("The id: " + id + " is already active.");            
        }
        else
        {
            if (setState(id,true))
            {
                lblmisc.setText("The id: " + id + " is now set to active.");
                btnActivate.setEnabled(false);
                btnDeactivate.setEnabled(false);
            }		 
        }
     } catch (SQLException e) {
          //They entered something wrong
			rtn = e.getMessage();
         txtEnterCategoryID.setText("");
         lblmisc.setText("The ID entered is not a valid Category Price Scheme.");
     }
  } 
}//GEN-LAST:event_btnActivateMouseClicked
/*
 * This method determines the State of the Price Scheme
 * It returns true if the Scheme is active
 * False if it is deactive
 */ 
private boolean determineState(String id)throws SQLException
{
         boolean state = false;
         String rtn = "";
         ResultSet set = null;
         Statement stmt = conn.createStatement();
         try
         {
           set = stmt.executeQuery("SELECT FROM CATEGORYPRICESCHEMEONE WHERE CATEGORYPRICESCHEME1ID = '" + id + "'"); 
         }
         catch (SQLException e) 
         {
              set = stmt.executeQuery("SELECT FROM  CATEGORYPRICESCHEMETWO WHERE CATEGORYPRICESCHEME2ID = '" + id + "'"); 
         }         
         if (set.getBoolean(5))
         {
             state =  true;
         }
         else
         {
             state = false;
         }
         stmt.close();
         return state;
}
/*
 * This method sets the state of the Price Scheme
 */ 
private boolean setState(String id, boolean state)
{
    boolean setOk = false;
    
    try {
			Statement stmt = conn.createStatement();
         try
         {
			stmt.executeQuery("UPDATE CATEGORYPRICESCHEMEONE SET ISACTIVE = " + 
                 state + " where CATEGORYPRICESCHEME1ID = '" + id + "'");					 
         stmt.close();
			setOk = true;
         }
         catch (SQLException e)
         {
              try
                 {
              stmt.executeQuery("UPDATE CATEGORYPRICESCHEMETWO SET ISACTIVE = " + 
                  state + " where CATEGORYPRICESCHEME2ID = '" + id + "'");					 
            	stmt.close();
            	setOk = true;
                 }
                catch(SQLException t)
                {
                 t.printStackTrace();
                	setOk = false;
                 }
         }
			
        } catch (SQLException e) {			 
			e.printStackTrace();
			setOk = false;
        }	   
    return setOk;
}
private void btnDeactivateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnDeactivateMouseClicked
if (btnDeactivate.isEnabled() == true)
{
    String rtn = "";
    String id = txtEnterCategoryID.getText();
    try
     {
        if (determineState(id))
        {
            if (setState(id,false))
            {
              lblmisc.setText("The id: " + id + " is now set to deactive.");
              btnActivate.setEnabled(false);
              btnDeactivate.setEnabled(false);
            }          
        }
        else
        {            
              lblmisc.setText("The id: " + id + " is already deactive.");              		 
        }
     }
    catch (SQLException e) 
     {
          //They entered something wrong
			rtn = e.getMessage();
         txtEnterCategoryID.setText("");
         lblmisc.setText("The ID entered is not a valid Category Price Scheme.");
     }
 }
}//GEN-LAST:event_btnDeactivateMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnActivate;
    private javax.swing.JButton btnDeactivate;
    private javax.swing.JLabel lblCategory;
    private javax.swing.JLabel lblmisc;
    private javax.swing.JTextField txtEnterCategoryID;
    // End of variables declaration//GEN-END:variables
Connection conn;
}
